package org.ovgu.coursify.data;

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStreamWriter;

import org.ovgu.coursify.HomeActivity;

import android.content.Context;

@SuppressWarnings("deprecation")
public class Profile {

	private String userName, password, type, name;
	private int[] lectures;
	private static Profile currentProfile;

	static {
		FileInputStream fis = null;
		final StringBuffer storedString = new StringBuffer();

		try {
			fis = HomeActivity.getContext().openFileInput("login.txt");
			DataInputStream dataIO = new DataInputStream(fis);
			String strLine = null;

			if ((strLine = dataIO.readLine()) != null) {
				storedString.append(strLine);
			}

			dataIO.close();
			fis.close();
		} catch (FileNotFoundException e) {
			currentProfile = null;
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (fis != null) {
				try {
					fis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}

				String data = storedString.toString();
				if (data != null) {
					String[] splited = data.split(",");
					String userName = splited[0];
					String password = splited[1];
					login(userName, password);
				}

			}
		}
	}

	private static void writeToFile(String data) {
		try {
			OutputStreamWriter outputStreamWriter = new OutputStreamWriter(
					HomeActivity.getContext().openFileOutput("login.txt",
							Context.MODE_PRIVATE));
			outputStreamWriter.write(data);
			outputStreamWriter.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static Profile getCurrentProfile() {
		return currentProfile;
	}

	/**
	 * Search for existing {@link Profile} in the data base
	 * 
	 * @return existing profile, or <code>null</code> if password and name don't
	 *         match
	 */
	public static Profile login(String userName, String password) {
		for (Profile profile : DataBase.getProfiles()) {
			if (profile.userName.equals(userName)) {
				if (profile.password.equals(password)) {
					currentProfile = profile;
					writeToFile(userName + "," + password);
					return profile;
				}
				break;
			}
		}
		return null;
	}

	public static boolean isLoggedIn() {
		return currentProfile != null;
	}

	public static void logout() {
		currentProfile = null;
		HomeActivity.getContext().deleteFile("login.txt");
	}

	Profile(String userName, String password, String type, String name,
			int[] lectures) {
		this.userName = userName;
		this.password = password;
		this.type = type;
		this.name = name;
		this.lectures = lectures;
	}

	/**
	 * Gets the URZ-account name of the user
	 */
	public String getUserName() {
		return userName;
	}

	public String getPassword() {
		return password;
	}

	public String getType() {
		return type;
	}

	/**
	 * Gets the real name of the user
	 */
	public String getName() {
		return name;
	}

	public int[] getLectures() {
		return lectures;
	}

	public final static String TYPE_STUDENT = "student";
	public final static String TYPE_Docent = "docent";
}
